package com.xnfh.field.ui.activity

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.View
import android.view.inputmethod.EditorInfo
import android.view.inputmethod.InputMethodManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.gyf.immersionbar.ktx.immersionBar
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener
import com.xnfh.common.bean.FieldListBean
import com.xnfh.field.R
import com.xnfh.field.databinding.ActivityFieldListLayoutBinding
import com.xnfh.field.ui.adapter.FieldListAdapter
import com.xnfh.field.viewmodel.FieldListViewModel
import com.xnfh.framework.base.BaseActivity
import com.xnfh.framework.base.BaseStateObserver

/**
 * @Description:
 * @Author: ziheng
 * @CreateDate: 2023/11/28 11:18
 * @UpdateUser: ziheng
 * @UpdateDate: 2023/11/28 11:18
 * @UpdateRemark: 更新说明
 */
class FieldListActivity : BaseActivity<ActivityFieldListLayoutBinding>(), View.OnClickListener {
    override fun getLayoutID(): Int = R.layout.activity_field_list_layout

    companion object {
        const val REQUEST_CODE = 1001
        const val RESPONSE_CODE = 10001
        fun start(context: Activity, bundle: Bundle? = null, requestCode: Int) {
            val intent = Intent(context, FieldListActivity::class.java)
            if (bundle != null) {
                intent.putExtra("bundle", bundle)
            }
            context.startActivityForResult(intent, requestCode)
        }
    }

    private var viewModel: FieldListViewModel? = null
    private var page = 1
    private var adapter: FieldListAdapter? = null
    override fun initImmersionBar() {
        immersionBar {
            statusBarDarkFont(true).statusBarColor(R.color.white) //状态栏字体是深色，不写默认为亮色
        }
    }

    override fun init(savedInstanceState: Bundle?) {
        setHeaderTitle(getString(R.string.field_list_select))
        mBind.fieldListSelect.setOnClickListener(this)
        mBind.fieldListRecycle.layoutManager = LinearLayoutManager(this)
        adapter = FieldListAdapter()
        mBind.fieldListRecycle.adapter = adapter
        mBind.fieldListSmart.autoRefresh()
        mBind.fieldListSmart.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {
            override fun onRefresh(refreshLayout: RefreshLayout) {
                page = 1
                viewModel?.requestFieldList(page, mBind.fieldListSearch.text.toString())
            }

            override fun onLoadMore(refreshLayout: RefreshLayout) {
                page++
                viewModel?.requestFieldList(page, mBind.fieldListSearch.text.toString())
            }
        })

        mBind.fieldListSearch.setOnEditorActionListener { v, actionId, event ->
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                mBind.fieldListSmart.autoRefresh()
            }
            val imm = getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager
            imm.hideSoftInputFromWindow(v.windowToken, 0) //强制隐藏键盘
            return@setOnEditorActionListener false
        }
        mBind.fieldListSearch.setOnDeleteClickListener {
            mBind.fieldListSmart.autoRefresh()
        }

        adapter?.setOnItemClickListener { adapter, view, position ->
            this.adapter?.setOnSelectItem(position)
        }

    }

    override fun observe() {
        viewModel = FieldListViewModel()
        viewModel?.fieldListData?.observe(this, object : BaseStateObserver<FieldListBean>(null) {
            override fun getRespDataSuccess(it: FieldListBean) {
                super.getRespDataSuccess(it)
                if (it.total <= 0) {
                    adapter?.setNewInstance(null)
                    adapter?.setEmptyView(com.xnfh.common.R.layout.adapter_empty_view)
                } else {
                    mBind.fieldListSmart.setNoMoreData(it.records.isNotEmpty())
                    if (it.pages.toInt() == 1) {
                        adapter?.setNewInstance(it.records.toMutableList())
                    } else {
                        adapter?.addData(it.records)
                    }
                    page = it.pages.toInt()
                }
                //关闭刷新状态
                mBind.fieldListSmart.let {
                    it.finishRefresh()
                    it.finishLoadMore()
                }
            }

            override fun getRespDataError() {
                super.getRespDataError()
                //关闭刷新状态
                mBind.fieldListSmart.let {
                    it.finishRefresh()
                    it.finishLoadMore()
                }
            }
        })

    }

    override fun onClick(v: View?) {
        when (v) {
            mBind.fieldListSelect -> {
                if (FieldListAdapter.currSelect == -1) return
                intent.putExtra("data", adapter?.getItem(FieldListAdapter.currSelect))
                setResult(RESPONSE_CODE, intent)
                finish()
            }
        }
    }
}